package com.wondertek.mist.utils;

import java.text.DecimalFormat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;



public class DownloadProgress extends Dialog {
	private Context ctx;
	private int fileTotalSize;
	private int downLoadFileSize;
	private String downloadPercent;
	private String downLoadedMB;	
	private String totalMBSize;
	
	public static int K = 1024;
	private static final int MB = 1024 * 1024;
	
	public DecimalFormat dFormat;
	public ProgressBar pb;
	public TextView left;
	public TextView right;
	public Button cancel;
	private String msg;
	private boolean isDownloadCancel = false;
	private boolean isShowCancelButton = false;
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (!Thread.currentThread().isInterrupted()) {
				switch (msg.what) {
				case 0:
					pb.setMax(fileTotalSize);
				case 1:
					pb.setProgress(downLoadFileSize);
					left.setText(downloadPercent + "%");
					right.setText(downLoadedMB + "/" + totalMBSize);
					break;
				case 2:
					DownloadProgress.this.dismiss();
					new AlertDialog.Builder(ctx).setTitle("提示信息").setMessage("下载完成！").setNegativeButton("确定",
						new OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
							}
						}).show();
					break;
				case -1:
					String error = msg.getData().getString("error");
					break;
				}
			}
			super.handleMessage(msg);
		}
	};
	
	private  class CancelButtonClickLstener implements  Button.OnClickListener{
		@Override
		public void onClick(View arg0) {
			isDownloadCancel = true;//标示取消下载
			DownloadProgress.this.setFinish();
			DownloadProgress.this.dismiss();
		}
	};

	public DownloadProgress(Context context) {
		this(context,"");
	}
	
	public DownloadProgress(Context context,String msg) {
		this(context,"正在下载",msg);
	}
	
	public DownloadProgress(Context context,String title,String msg) {
		super(context);
		this.ctx = context;
		this.setCancelable(false);
		this.setTitle(title);
		this.msg = msg;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(RUtil.layout(this.getContext(),"download_progress"));
		dFormat = (DecimalFormat) DecimalFormat.getInstance();
		dFormat.applyPattern("#0.0");
		TextView msgView = (TextView)findViewById(RUtil.id(this.getContext(),"progress_dialog_msg"));
		if(!StringUtils.isEmpty(msg)) {
			msgView.setText(msg);
		}
		pb = (ProgressBar) findViewById(RUtil.id(this.getContext(),"down_pb"));
		left = (TextView) findViewById(RUtil.id(this.getContext(),"leftText"));
		right = (TextView) findViewById(RUtil.id(this.getContext(),"rightText"));
		
		if(isShowCancelButton){
			cancel = (Button) findViewById(RUtil.id(this.getContext(),"cancelButton"));
			cancel.setVisibility(View.VISIBLE);
		    cancel.setOnClickListener(new CancelButtonClickLstener());
		}
	}
	
	public void refresh(int downloadSize, int flag) {
		if (flag == 1) {
			downLoadFileSize = downloadSize;
			downloadPercent = dFormat.format(downloadSize * 100.0 / fileTotalSize);
			downLoadedMB = dFormat.format(downloadSize * 1.00 / MB) + "M";
		} else if (flag == 0) {
			fileTotalSize = downloadSize;
			totalMBSize = dFormat.format(fileTotalSize * 1.00 / MB) + "M";
		}
		Message msg = new Message();
		msg.what = flag;
		handler.sendMessage(msg);
	}

	public void setFinish() {
		pb.setProgress(fileTotalSize);
	}

	public boolean isDownloadCancel() {
		return isDownloadCancel;
	}

	public void setShowCancelButton(boolean isShowCancelButton) {
		this.isShowCancelButton = isShowCancelButton;
	}
}

